<?php
/**
* File : EntiteFactory.php
* Created by Ludovic Reenaers
* Created on  24 nov. 2010
* lreenaers@hotmail.com
*/
class EntiteFactory{
	private $Entite;
	private $Translator;
	private $params;
	private $Factory;
	function __construct($Entite,$translator,$params,$factory) {
		if(is_a($Entite, 'Entite')){
			$this->Entite = $Entite;
		}else{
			throw new Exception("Not an Entite");
		}
		if(is_a($factory, 'DAOFactory')){
			$this->Factory = $factory;
		}else{
			throw new Exception("Not a DAOFactory");
		}
		if(is_a($translator, 'DBTranslator')){
			$this->Translator = $translator;
		}else{
			throw new Exception("Not a DBTranslator");
		}
		$this->params = $params;
	}
	function dump(){
		$str = "";
		$type = $this->Entite->getTypeEntite();
		$genName = ucfirst($type->getLabel())."Generator";
		if(file_exists($_SERVER{'DOCUMENT_ROOT'}."/generators/".$genName.".php")){
			$mainGen = new $genName($this->Translator,$this->params,$this->Factory);
			foreach ($this->Entite->getPropriete() as $Propriete){
				$propGenName = ucfirst($Propriete->getTypePropriete()->getLabel())."Generator";
				if(file_exists($_SERVER{'DOCUMENT_ROOT'}."/generators/".$propGenName.".php")){
					$propGen = new $propGenName($this->Translator,$this->params,$this->Factory);
					$mainGen->addChild($propGen);
				}else{
					throw new Exception("No generator found for this Propriete: ".$propGenName);	
				}
			}
			$str= $mainGen->dump();
		}else{
			throw new Exception("No generator found for this Entite:".$genName);
		}
		return $str;
	}
}
?>